package lt.marsrutai.mobile.android.activity;

import java.util.List;

import lt.marsrutai.mobile.android.R;
import lt.marsrutai.mobile.android.activity.intent.FastestRoutesSearchIntentHolder;
import lt.marsrutai.mobile.android.map.overlay.ClickableBalloonView;
import lt.marsrutai.mobile.android.map.overlay.ClickableOverlayItem;
import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.location.Location;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;

/**
 * Sub-activity for selecting a point on a map 
 * 
 * @author Justas
 */
public class PointSelectionMapActivity extends BaseMapActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        MapOverlay mapOverlay = new MapOverlay(this);
        List<Overlay> listOfOverlays = mapView.getOverlays();
        listOfOverlays.add(mapOverlay);
    }

    /**
     * Overlay for catching tap events anywhere on the map
     * 
     * @author Justas
     */
    private static class MapOverlay extends Overlay {
        
        private Activity activity; 
        private ClickableBalloonView balloon;
        
        private MapOverlayClickListener clickListener;

        public MapOverlay(Activity activity) {
            this.activity = activity;
            clickListener = new MapOverlayClickListener(activity);
        }
        
        @Override
        public boolean onTap(final GeoPoint point, MapView mapView) {
            MapView.LayoutParams layoutParams = new MapView.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, point,
                    MapView.LayoutParams.BOTTOM_CENTER);
            layoutParams.mode = MapView.LayoutParams.MODE_MAP;
            clickListener.setGeoPoint(point);
            
            if (balloon == null) {
                balloon = new ClickableBalloonView(activity);
                balloon.setData(new ClickableOverlayItem(point, activity.getString(R.string.tap_to_select_point), null, clickListener));
                mapView.addView(balloon, layoutParams);
            }
            else {
                balloon.setLayoutParams(layoutParams);
            }

            balloon.setVisibility(View.VISIBLE);
            
            return true;
        }
    }

    private static class MapOverlayClickListener implements OnClickListener {
        
        private Activity activity;
        private GeoPoint geoPoint;
        
        public MapOverlayClickListener(Activity activity) {
            this.activity = activity;
        }
        
        @Override
        public void onClick(View v) {
            FastestRoutesSearchIntentHolder intentHolder = new FastestRoutesSearchIntentHolder(new Bundle());
            Location location = new Location((String) null);
            location.setLatitude(geoPoint.getLatitudeE6() / 1E6d);
            location.setLongitude(geoPoint.getLongitudeE6() / 1E6d);
            intentHolder.setSelectedLocation(location);
            
            activity.setResult(RESULT_OK, intentHolder.getIntent());
            activity.finish();
        }

        public void setGeoPoint(GeoPoint point) {
            this.geoPoint = point;
        }
    }
}
